package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeginClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeginClient dictFeginClient;


    @Override
    public void save(Map<String, Object> stringObjectMap) {
//        先将集合的json数据转换成String对象,再将String转换成对象hospital
        String mapString = JSONObject.toJSONString(stringObjectMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        // 判断是否存在数据(根据hoscode查询数据)
        String hoscode = hospital.getHoscode();
        Hospital hospitalExits = hospitalRepository.getHospitalByHoscode(hoscode);

        if(hospitalExits != null){
            //执行修改操作
            hospital.setStatus(hospitalExits.getStatus());
            hospital.setCreateTime(hospitalExits.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            // 不存在进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscoe(String hoscode) {

        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospitalByHoscode;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //将hospitalQ取Vo转变成hospital
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        // 创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        // 调用方法实现
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        // 获取查询的list集合,遍历进行医院等级的封装
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHostype(item);
        });
        return pages;
    }

    /**
     * 更新医院上线状态
     * @param id 医院id
     * @param status 医院状态
     */
    @Override
    public void updateStatus(String id, Integer status) {

        // 根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        // 设置需要修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHostype(hospitalRepository.findById(id).get());
        //医院基本信息(包含医院等级)
        result.put("hospital",hospital);
        //单独处理更直观
        result.put("bookingRule",hospital.getBookingRule());
        // 不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }

    // 根据医院编号获取医院名称
    @Override
    public String getHospMame(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital!=null){
            return  hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {

        Map<String,Object> result = new HashMap<>();

        //调用方法获取医院全部数据
        Hospital hospital = setHospitalHostype(this.getByHoscoe(hoscode));
        result.put("hospital",hospital);

        // 预约规则
        result.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }

    // 获取医院list，对医院等级进行封装
    private Hospital setHospitalHostype(Hospital hospital) {

        // 根据dictCode和value查询医院等级
        String hosTypeString = dictFeginClient.getName("Hostype", hospital.getHostype());
        // 查询省市地区
        String provinceString = dictFeginClient.getName(hospital.getProvinceCode());
        String cityString = dictFeginClient.getName(hospital.getCityCode());
        String districtString = dictFeginClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hosTypeString);
        return hospital;

    }
}